package com.atguigu.gulimall.ware.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class WareRabbitConfig {
    /**
     *
     * @return
     */
    @Bean
    public MessageConverter messageConverter(){
        return new Jackson2JsonMessageConverter();
    }


    @Bean
    public Exchange stocKEventExchange(){
        //String name, boolean durable, boolean autoDelete, Map<String, Object> arguments
        TopicExchange topicExchange = new TopicExchange("stock-event-exchange",true,false,null);
        return topicExchange;
    }

    @Bean
    public Queue stockDelayQueue(){
        //String name, boolean durable, boolean exclusive, boolean autoDelete, Map<String, Object> arguments
        Map<String,Object> arguments = new HashMap<>();
        arguments.put("x-dead-letter-exchange","order-event-exchange");
        arguments.put("x-dead-letter-routing-key","order.release.order");
        arguments.put("x-message-ttl",50000);
        Queue queue = new Queue("stock.delay.queue", true, false, false, arguments);
        return queue;
    }

    @Bean
    public Queue stockReleseStockQueue(){
        Queue queue = new Queue("stock.relese.queue", true, false, false, null);
        return  queue ;
    }

    @Bean
    public Binding stockLocked(){
        //String destination, DestinationType destinationType, String exchange, String routingKey,
        //			Map<String, Object> arguments
        Binding binding = new Binding("stock.delay.queue", Binding.DestinationType.EXCHANGE, "stock-event-exchange", "stock.locked", null);
        return binding;
    }

    @Bean
    public Binding stockLockedToExchange(){
        //String destination, DestinationType destinationType, String exchange, String routingKey,
        //			Map<String, Object> arguments
        Binding binding = new Binding("stock-event-exchange", Binding.DestinationType.EXCHANGE, "stock-event-exchange", "stock.locked", null);
        return binding;
    }

    @Bean
    public Binding stockDelary(){
        //String destination, DestinationType destinationType, String exchange, String routingKey,
        //			Map<String, Object> arguments
        Binding binding = new Binding("stock.relese.queue", Binding.DestinationType.EXCHANGE, "stock-event-exchange", "stock.release", null);
        return binding;
    }

    @Bean
    public Binding stockRelease(){
        Binding binding = new Binding("stock-event-exchange", Binding.DestinationType.EXCHANGE, "stock-event-exchange", "stock-release", null);
        return binding;
    }

    @Bean
    public Binding stockReleaseStock(){
        Binding binding = new Binding("stock-event-exchange", Binding.DestinationType.EXCHANGE, "stock-event-exchange", "stock-release", null);
        return binding;
    }
}
